<template>
  <div class="_wrapper _flex _flex-a-start _p20 _flex-start _flex-space">
    <el-time-select v-model="currentConfig.time" start="00:00" step="00:30" end="23:30" placeholder="请选择时间" />
    <el-button type="primary" @click="save()">保存</el-button>
    <el-button type="danger" @click="clear()">清空</el-button>
  </div>
</template>

<script>
  import hkApi from './ApiHaikang';

  export default {
    props: {
      deviceCode: {
        type: String,
        require: true
      },
      presetPointIndex: {
        type: Number,
        require: true
      },
    },
    data() {
      return {
        defaultConfig: [],
        currentConfig: {}
      }
    },
    mounted() {
      hkApi.saveIotDeviceConfig(this.deviceCode).then(res => {
        if (res.data && res.data instanceof Array) {
          this.defaultConfig = res.data;
          this.initCurr();
        } else {
          this.initCurr();
        }
      })
    },
    methods: {
      clear() {
        this.currentConfig.time = "";
        this.save();
      },
      initCurr() {
        this.currentConfig = this.defaultConfig.find(item => item.presetPointIndex == this.presetPointIndex);
        if (!this.currentConfig) {
          this.currentConfig = {
            time: "",
            presetPointIndex: this.presetPointIndex
          }
          this.defaultConfig.push(this.currentConfig);
        }
      },
      save() {
        hkApi.saveIotDeviceConfig(this.deviceCode, this.defaultConfig).then(res => {
          this.$emit("close");
        })
      }
    },
  }
</script>

<style>
</style>
